package com.encryption.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.encryption.dao.Condition;
import com.encryption.dao.DepartmentDAO;
import com.encryption.model.Department;
import com.encryption.service.DepartmentService;
import com.encryption.service.ca.impl.InitDeptCertInfoImpl;
/**
 * 部门管理接口实现类
 * @author Administrator
 *
 */
public class DepartmentServiceImpl implements DepartmentService {
	
	private static final Logger log = LoggerFactory.getLogger(DepartmentServiceImpl.class);


	private static DepartmentServiceImpl departmentServiceImpl = null;
	
	
	public static  DepartmentServiceImpl getDepartmentServiceImpl(){
		if(null == departmentServiceImpl) {
			departmentServiceImpl = new DepartmentServiceImpl();
		} 
		return departmentServiceImpl;
	}
	
	/**
	 * 添加部门 
	 * @param Department
	 */
	public boolean addDepartment(Department department){
		try {
			DepartmentDAO.getDepartmentDAO().save(department);
			
			InitDeptCertInfoImpl impl = new InitDeptCertInfoImpl();
			impl.addDeptCert(department);
		} catch (Exception e) {
			log.error("添加部门异常",e);
			return false;
		} 
		return true;
	}
	
	/**
	 * 编辑部门
	 * @param Department
	 * @return
	 */
	public  boolean updateDepartment(Department department){
		try {
			DepartmentDAO.getDepartmentDAO().merge(department);
		} catch (Exception e) {
			log.error("编辑部门异常",e);
			return false;
		}
		return true;
	}
	
	
	/**
	 * 删除部门
	 * @param id
	 */
	public boolean deleteDepartment(String id){
		try {
			DepartmentDAO.getDepartmentDAO().
			delete(DepartmentDAO.getDepartmentDAO().findById(Integer.parseInt(id)));
		} catch (Exception e) {
			log.error("删除部门异常",e);
			return false;
		}
		return true;
	}
	
	/**
	 * 查询部门列表
	 * @return
	 */
	public List<Department> getDepartmentByCondition(List<Condition> conditions){
		return DepartmentDAO.getDepartmentDAO().findByExample(conditions);
	}
	/**
	 * 分页查询部门列表
	 * @param instance
	 * @param page
	 * @return
	 */
	public List<Department> getDepartmentPagingByCondition(List<Condition> condList, int page, int pageSize){
		return DepartmentDAO.getDepartmentDAO().findByExample4Page(condList,page,pageSize);
	}
	
	/**
	 * 获取单个部门,根据Departmentid
	 * @param DepartmentId
	 * @return
	 */
	public Department getDepartmentById(String id){
		return DepartmentDAO.getDepartmentDAO().findById(Integer.parseInt(id));
	}
	/**
	  * 根据登录名获取部门
	  * @param Name
	  */
	public Department getDepartmentByLoginName(String name){
		return (Department)(DepartmentDAO.getDepartmentDAO().findByProperty("name",name).get(0));
	}

	@Override
	public List<Department> getAll() {
		return DepartmentDAO.getDepartmentDAO().findAll();
	}
}
